<?php
/**
 * 后台_角色_控制器
 * @property Admin_role_mdl $role
 * @property Admin_menu_mdl $menu
 */
class Role extends Admin_Controller{
	public function __construct(){
		parent::__construct();
		$this->load->helper('category');
		$this->load->model('Admin_role_mdl','role');
		$this->load->model('Admin_menu_mdl','menu');
	}

	public function index(){
		$list = $this->role->getRoleMenuList();
		$menuList = $this->menu->getList();
		$menuList = array_combine( array_column($menuList, 'id') , $menuList);
		$temp['list'] = $list;
		$temp['menuList'] = $menuList;
		$this->showView('admin/system/role_index',$temp);
	}

	public function add(){
		if (empty($_POST)) {
			$temp['title'] = '增加角色';
			$this->_showEdit($temp);
			return ;
		}
		$input = $this->_getEditData();
// 		pr($data);die;
		$name = $input['name'];
		$menuIdList = $input['menus'];
		$role = array(
				'name'=>$name,
		);
		$res = 0;
		$roleID = $this->role->insert($role);
		if( $roleID ){
			if( ! $this->role->addMenu2Role($roleID, $menuIdList) ){
				$res+=2;
			}
		}else{
			$res += 1;
		}
		if($res==0){
			alert('增加角色成功',ADMIN_DIR.'role/index');
		}else{
			alert('增加角色失败:'.$res,ADMIN_DIR.'role/index');
		}
	}

	public function update(){
		$roleID = (int)$this->input->get('rid',TRUE);
		$role = $this->role->getRoleDetail($roleID);
		if( empty($role) ){
			alert('无此角色','back');
			exit();
		}
		if( empty($_POST) ){
			$temp['role'] = $role;
			$temp['title'] = '修改角色权限';
			$this->_showEdit($temp);
			return ;
		}
		$input = $this->_getEditData();
		$res = 0;
		if( $role['name'] != $input['name'] ){
			$where = array();
			$update = array();
			$where['id'] = $roleID;
			$update['name'] = $input['name'];
			if( ! $this->role->update($where,$update) ){
				$res +=1;
			}
		}
		if( $role['menuIdList'] != $input['menus'] ){
			$delMenuIdList = array_diff($role['menuIdList'], $input['menus']);
			$addMenuIdList = array_diff($input['menus'], $role['menuIdList']);
			if( !empty($delMenuIdList) ){
				if( ! $this->role->deleteMenu2Role($roleID, $delMenuIdList) ){
					$res +=2;
				}
			}
			if( !empty($addMenuIdList) ){
				if( ! $this->role->addMenu2Role($roleID, $addMenuIdList) ){
					$res +=4;
				}
			}

		}
		if($res==0){
			alert('增加角色成功',ADMIN_DIR.'role/index');
		}else{
			alert('增加角色失败:'.$res,'back');
		}
	}

	public function delete(){
		$roleID = (int)$this->input->get('rid',TRUE);
		$role = $this->role->getRoleDetail($roleID);
		if( empty($role) ){
			alert('无此角色','back');
			exit();
		}

		$res = $this->role->deleteRole($roleID);
		if($res){
			alert('删除角色成功',ADMIN_DIR.'role/index');
		}else{
			alert('删除角色失败',ADMIN_DIR.'role/index');
		}

	}

	private function _showEdit($temp=array()){
		$menus = $this->menu->getList();
		$menus = Category::unlimitedForLevel($menus);

		$temp['menus'] = $menus;
		$this->showView('admin/system/role_edit',$temp);
	}

	private function _getEditData(){
		$input = &$_REQUEST;
		$input['name'] = trim($input['name']);
		$roleID = isset($input['rid']) ? (int)$input['rid'] : NULL;
		if( $this->role->checkRoleNameIsExists($input['name'],$roleID) ){
			alert('角色名称已经存在','back');
			exit;
		}
		return $input;
	}






// 	public function _add(){
// 		if( empty($_POST) ){
// 			$menus = $this->menu->getList();
// 			$menus = Category::unlimitedForLevel($menus);

// 			$temp['menus'] = $menus;

// 			$this->load->view('admin/system/role_add',$temp);
// 			return ;
// 		}

// 		$input = &$_REQUEST;
// 		$data['name'] = $input['name'];
// 		$data['menus'] = implode(',',$input['menus']);

// 		if($this->role->insert($data)){
// 			alert('增加角色成功',ADMIN_DIR.'role/index');
// 		}else{
// 		    $this->output->enable_profiler(TRUE);die;
// 			alert('增加角色失败',ADMIN_DIR.'role/index');
// 		}


// 	}
// 	public function update(){
// 		$rid = $this->input->get('rid',TRUE);
// 		if( empty($_POST) ){
// 			$menus = $this->menu->getList();
// 			$menus = Category::unlimitedForLevel($menus);

// 			$detail = $this->role->getOneById($rid);

// 			$temp['menus'] = $menus;
// 			$temp['detail'] = $detail;



// 			$this->load->view('admin/system/role_add',$temp);
// 			return ;
// 		}

// 		$input = &$_REQUEST;
// 		$data['name'] = $input['name'];
// 		$data['menus'] = implode(',',$input['menus']);
// 		$where = array('id'=>$rid);
// 		if($this->role->update($where,$data)){
// 			alert('修改角色成功',ADMIN_DIR.'role/index');
// 		}else{
// 		    $this->output->enable_profiler(TRUE);die;
// 			alert('修改角色失败',ADMIN_DIR.'role/index');
// 		}

// 	}
// 	public function delete(){
// 		$rid = $this->input->get('rid',TRUE);
// 		$where = array('id'=>$rid);
// 		if($this->role->delete($where)){
// 			alert('删除角色成功',ADMIN_DIR.'role/index');
// 		}else{
//             // $this->output->enable_profiler(TRUE);
// 			alert('删除角色失败',ADMIN_DIR.'role/index');
// 		}
// 	}
}
